import {AfterViewInit, Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {Subscription} from 'rxjs';
import {MessageService, ProgressBarComponent} from '../../../../ng-tools';



@Component({
  selector: 'app-content',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.scss'],
})
export class ContentComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild(ProgressBarComponent) progress: ProgressBarComponent;
  private subscription = new Subscription();

  widthOffset = 50;
  heightOffset = 55;
  breadcrumb;

  constructor(
    private message: MessageService,
  ) {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    this.messageHandler();
  }

  messageHandler() {
    const breadcrumbSub = this.message.getMessage('breadcrumb changed', this).subscribe((next) => {
      this.breadcrumb = next.data;
    });
    const progressSub = this.message.getMessage('page load progress', this).subscribe((next) => {
      if (next.data) {
        this.progress.start(next.data);
      } else {
        this.progress.complete();
      }
    });

    this.subscription.add(breadcrumbSub);
    this.subscription.add(progressSub);
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
}
